﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ACCPS1ProjectDemo.ControlDemo
{
    public partial class DateTimePickerDemo : UserControl
    {
        public DateTimePickerDemo()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime riQi = dateTimePicker4.Value; //包含日期和时间两部分
            MessageBox.Show("控件值：" + riQi);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DateTime riQi = dateTimePicker4.Value.Date; //包含日期和时间两部分，但时间为0
            string formatRiQi = dateTimePicker4 .Value.Date.ToString("yyyy-MM-dd");
            MessageBox.Show("控件值：" + riQi+"\n格式化后："+formatRiQi);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dateTimePicker4.Value = DateTime.Parse(textBox1.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            TimeSpan time = dateTimePicker4.Value.TimeOfDay; //只获取时间部分，注意数据类型
            MessageBox.Show("控件值：" + time);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //获取当前时间
            MessageBox.Show(DateTime.Now.ToString());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DateTime dt = dateTimePicker4.Value;
            string msg = string.Format("当前时间： \n年：{0} \n月：{1} \n日：{2} \n时：{3}  \n分：{4}  \n秒：{5}",
                dt.Year,   //年
                dt.Month,   //月
                dt.Day,  //日
                dt.Hour,  //时
                dt.Minute,  //分
                dt.Second);  //秒
            MessageBox.Show(msg);

            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            TimeSpan sp = DateTime.Now - dateTimePicker4.Value;
            string msg = "间隔：" + sp;
            msg += "\n间隔天数：" + sp.Days;
            msg += "\n间隔小时：" + sp.Hours;
            MessageBox.Show(msg);
        }
    }
}
